import { defineStore } from 'pinia';
import { getAuthCache, setAuthCache } from '/@/utils/auth';

interface StateTree {
  isNext: boolean,
  currentTopic: object | any,
  loading: boolean,
  countList: Array<any>,
  questions: object | any,
  dfdSelected: object | any,
  tagsList: Array<any>,
}


export const useTeacherMarkStore = defineStore({
  id: 'teacherMarkStore',
  state: (): StateTree => ({
    isNext: false,
    currentTopic: {},
    loading: false,
    countList: [],
    questions: '',
    dfdSelected: {},
    tagsList: [],
  }),
  getters: {
    getQuestions(): object | any {
      return this.questions || getAuthCache('testQuestions');
    },
    getCountList(): Array<any> {
      return this.countList;
    },
    getNext(): boolean {
      return this.isNext;
    },
    getCurrentTopic(): object | any {
      return this.currentTopic;
    },
    getLoading(): boolean {
      return this.loading;
    },
    getDfdSelected(): object | any {
      return this.dfdSelected;
    },
    getTagsList(): Array<any> {
      return this.tagsList;
    },
  },
  actions: {
    setLoading(val) {
      this.loading = val;
    },
    setDfdSelected(val) {
      this.dfdSelected = val;
    },
    setNext(val) {
      this.isNext = val;
    },
    setCurrentTopic(topic) {
      this.currentTopic = topic || {};
    },

    setCountList(arr) {
      this.countList = arr;
    },
    setQuestions(val) {
      this.questions = val;
      setAuthCache('testQuestions', val)
    },
    setTagsList(type, val) {
      if (type === 'new') {
        this.tagsList = [val]
      } else if (type === 'add') {
        this.tagsList.push(val)
      } else if (type === 'delete') {
        this.tagsList = this.tagsList.filter(v => v.pyDfdId != val.pyDfdId)
      } else if (type === 'pop') {
        if (this.tagsList.length == 0) return false;
        this.tagsList.pop()
      } else if (type === 'update') {
        this.tagsList = this.tagsList.filter(v => v.pyDfdId != val.pyDfdId)
        this.tagsList.push(val)
      } else {
        this.tagsList = []
      }
      // 计算总分
      if (this.tagsList.length == 0) {
        this.currentTopic.score = null
      } else {
        this.currentTopic.score = 0
        this.tagsList.forEach(ele => {
          this.currentTopic.score += ele.value
        })
      }
      
      return true
    }
  },
});
